package com.ocom.webapi;



//本地部署加密类
// RSA私钥签名   公钥验签
// 设备指纹

import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;

public class LocalInstallEnc {

    private static String priKey = "";

    private static String pubKey = "";



    public static void main(String[] args){
        // 1. 待签名的原始数据
        String originalData = "ABE770490CAB58A173914B98D2EC3259";

        // 2. 替换为上一步生成的你的私钥
        String privateKey = "MIIFHDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIeCNjsl85ehECAggA\n" +
                "MAwGCCqGSIb3DQIJBQAwFAYIKoZIhvcNAwcECBCoFVZFD42JBIIEyM+EpoJbpZ9d\n" +
                "/LyedlDTjF8kVkKaf+kFHXNz0zwNwrd34+CsaMm4OQEZHiPLvK9db+ocNCNBla70\n" +
                "LmqSeVlVba49kuWqaBPT90CTbHIYRt7QrLktYvOeYtAZXbppM9b66wXU/KlmzNXn\n" +
                "nOIsrKWmGeX5XsAs+XfmX3ULNXKOgGWHzPp3abKzpoT23eSmPEhAwFjslMbwhHj/\n" +
                "XxKyA82/Y6MCpPVgB/zr3HexTAQx9JTeNkLHP+kJKkT0OKtKBCDDzsDuQr4cels2\n" +
                "ch2x96KhFxt80iJr8/3iNUP1iLhQoO60D559/wpa0XfEJViK176mhT1qZwhT8Vb9\n" +
                "5bRbCQsLH467izZh4iFf/9Uip/Fv2MpFoNuc1EhZ/T3PAeqPz+HyNIRCXjF/1HSh\n" +
                "UFN/ZkeuUZ0j7Jv3BKF2eAUuAbTExoL8w77Rgc3RvEYXFcEL45/UVPdgcdxO0mgi\n" +
                "5dt59UgQN0TTBSqFgQGT3JXJHVx00KCFOcOFnlwSYR46KiuqrxVmiErhBN5k/A4e\n" +
                "U7jIl1v85TON7KmeqGbnvQL19j4XlVDCPdkFK0D7inoKJSWmj2Hvomd1vbnv8Dlu\n" +
                "cDNqfj8Q3DePKWvZXbEcJOu4Y8ZP2nL3dCIYKrX7tsEPCL2LExKdzSt1sdOOMjgz\n" +
                "SlbyF+qqAVm96lpAnq3aS4KlwngdXCTYF59Er/mgfEp0bYEKSh4ygUpRP0zQRvWm\n" +
                "zZe44541l0FQPdJzZJinKiRh010Ss7BkE+oa2PgSwiMUZCH7lMhzBQWMRKG90jVV\n" +
                "jqQE2Uc0Ijz+Udj44Sqltk3/85g7NdeBrwrTVtymjv52Q5oLaNlboFNHKubYF9Zg\n" +
                "CSXyV1z6hBGjdgf/slJ8N45LsQaTP3yYGIWabWpydM/qpwCNSlPk4TNq3+lhQyiZ\n" +
                "ypXGELrJfqEtR5Zi+nJdBLhg+SP0FpbCKi8sNuoqbnWL44Gbcjmr2h33cZlzJWZi\n" +
                "SuctH8r88nvfDGwcPpg5XC4JL/siw5ttwNsevV5jKJpBB0A1Kc2Xx4bDXTUfKDtj\n" +
                "oXcd3O90eyXOklhYWhs+lx4+niqQc8meImruTHlnhCKOM0hh1D9ud/ePjzbpEBbN\n" +
                "FXxUZ1ocLgaRTOaNa+cQypjn6fvB7dB5D4SlP97tAqU1n3V7ClMVsJGhivnpEZaK\n" +
                "RlWFNhuivQLMrJjBbZeyvfDiqSC09py4j5rlABJNxSPqdUpxN67lQoEshj8648Mo\n" +
                "gAHSJFtbp7teR2LE7dyf/nkNgKy4zuNxVrj44NJc1q5amlIvDnTCjUf5kYHcWumH\n" +
                "LEqgdFF/tjV+AJ5kFMOAhPXJr7JJeX3YCVnCsg+gL8n9OjTzTY+gm9VMKhad53yX\n" +
                "WWtVVPvPyCQagm7yg6B/Z0N0cnFao4ZcfgkusqrbchBl23Pu2wdTjUhJZvMsNBsx\n" +
                "giMb39o0z0ogz2OAGWb0FaHY8Ndvq5+7jatb4RCHKr6BuxM7hM3sscsw+WP5m5QW\n" +
                "tGUcCSomSmdfhJ0I1JDzNqRyKT+ad8IdZu7EvEVLzFoLoE+WoiDOeRVTO57uSCjj\n" +
                "HqUfrLiLHni25Rb//eMTKb2veEhDhq7SJUjaKozaE0jJ0QjyKFmUl7Ne1oPdUzkm\n" +
                "PElzBW0iAatjkQ/kE59Obg==";

        // 3. 初始化签名对象，指定算法为SHA256withRSA，并传入私钥
        Sign sign = new Sign(SignAlgorithm.SHA256withRSA, privateKey, null);

        // 4. 对数据进行签名，得到Base64编码的签名结果
        byte[] signBytes = sign.sign(originalData.getBytes());
        String signature = cn.hutool.core.codec.Base64.encode(signBytes);

        System.out.println("原始数据: " + originalData);
        System.out.println("生成的签名: " + signature);

        // 在实际场景中，你需要将 originalData 和 signature 一起发送给接收方



        // 1. 接收到的原始数据和签名
        String receivedData = "这是一条需要验证的重要消息";
        String receivedSignature = "收到的Base64编码的签名字符串";

        // 2. 发送方的公钥
        String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqjF783L7pXtIl0//g3NR\n" +
                "yc2mKa5pieZlXJ6gkXdD0iGMbbb/vfGL2LNhaoZ+WfFx+JA345tBVkvbA692D5GC\n" +
                "XhaY+LFp7gQR/2BST3mPDiWRic5x+cH1dA1Uj1pq5Uz7jShpFxN/JGdbZb9KlZVR\n" +
                "vBjKdsyh0L6dSSxvHtRSWU/6EQAWc24lBs1ILUftVKOi+wjEWosva5pUoPwvQJFa\n" +
                "sFRCrieHK0V/SyCeH9PmIJXUAL/wtuWzmBsSF0nAPV7ipXU7l3nvP7QraskekKQm\n" +
                "CsD+stN0lLqYFQM98rdIGvaP4mQtW0fxnY8UMOzLnGzAdmPdmO9B0BOCn4Q2LIYi\n" +
                "SwIDAQAB";

        // 3. 初始化签名对象，传入公钥
        Sign sign2 = new Sign(SignAlgorithm.SHA256withRSA, null, publicKey);

        // 4. 进行验签
        // 先将接收到的签名解码为字节数组
        byte[] signBytes2 = cn.hutool.core.codec.Base64.decode(receivedSignature);
        boolean isValid = sign2.verify(receivedData.getBytes(), signBytes2);

        if (isValid) {
            System.out.println("✅ 验签成功！数据完整且来源可信。");
        } else {
            System.out.println("❌ 验签失败！数据可能被篡改或来源不正确。");
        }
    }

}
